<template>
  <div class="editname-box">
    <Field v-model="username" placeholder="请输入用户名" />
    <div class="button-box">
      <button v-if="type == 'ltdName'" @click="ltdNameSave">保存</button>
      <button v-else @click="saveBtn">保存</button>
    </div>
  </div>
</template>

<script>
import { Field } from "vant";
import { updateUser, getUserDetails } from "api/User";
import { updateEnterprise } from "api/Enterprise"

export default {
  components: {
    Field,
  },
  data() {
    return {
      username: "",
      type: ''
    };
  },
  methods: {
    // 昵称修改
    saveBtn() {
      updateUser({ userName: this.username }).then(({ code, message }) => {
        if (!code) {
          getUserDetails().then(({ data }) => {
            localStorage.userInfo = JSON.stringify(data);
            this.$router.go(-1);
          }); 
        } else {
          this.$toast(message);
        }
      });
    },
    // 修改公司名称
    ltdNameSave() {
      updateEnterprise({ enterpriseName: this.username, pkId: localStorage.enterpriseId }).then(({ code, message }) => {
        if (!code) {
          this.$toast("更改成功！");
          this.$router.go(-1);
        } else {
          this.$toast(message);
        }
      });
    }
  },
  mounted() {
    const name = this.$route.query.name;
    const type = this.$route.query.type;
    this.type = type;

    // 修改公司名称
    if (type == "ltdName") {
      this.username = name;
    }
    // 修改昵称 
    else {
      if (name) {
        this.username = name;
      }
    }
  },
};
</script>

<style lang="stylus" scoped>
@import './../../assets/styl/mixin.styl'
.editname-box
  background #F5F5F5
  height 100vh
  .button-box
    margin rem(25) auto
    text-align center
    button
      height rem(49)
      width rem(345)
      border-radius rem(5)
      background $baseColor
      color $infoColor
</style>